package com.ztgf.commodity.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.common.config.YmlConfig;
import com.ztgf.commodity.mapper.CommodityLabelMapper;
import com.ztgf.commodity.models.bo.CommodityLabelSaveBO;
import com.ztgf.commodity.models.dto.CategoryBrandAttributesRelationDTO;
import com.ztgf.commodity.models.dto.CategoryLabelRelationDTO;
import com.ztgf.commodity.models.dto.CommodityLabelDTO;
import com.ztgf.commodity.models.service.ICommodityLabelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.ztgf.common.utils.UtilConstants.isNull;

@Slf4j
@DubboService
public class CommodityLabelServiceImpl extends ServiceImpl<CommodityLabelMapper, CommodityLabelDTO> implements ICommodityLabelService {

    @Resource
    private YmlConfig ymlConfig;
    @Autowired
    private CategoryLabelRelationServiceImpl categoryLabelRelationService;
    @Autowired
    private CategoryBrandAttributesRelationServiceImpl categoryBrandAttributesRelationService;

    @Override
    public boolean addLabel(CommodityLabelSaveBO saveBO) {
        CommodityLabelDTO insertDto = new CommodityLabelDTO();
        insertDto.setName(saveBO.getName());
        insertDto.setStatus(0);
        if (StringUtils.isNotBlank(saveBO.getNotes())) {
            insertDto.setNotes(saveBO.getNotes());
        }
        insertDto.setId(ymlConfig.getPkId());
        return this.insert(insertDto);
    }

    @Override
    public boolean updateLabel(CommodityLabelSaveBO bo) {
        CommodityLabelDTO select = this.selectById(bo.getId());
        if (null == select) {
            return false;
        }
        CommodityLabelDTO updateDto = new CommodityLabelDTO();
        updateDto.setName(bo.getName());
        updateDto.setStatus(0);
        if (null != bo.getNotes()) {
            updateDto.setNotes(bo.getNotes());
        }
        updateDto.setId(select.getId());
        return this.updateById(updateDto);
    }

    @Override
    public List<CommodityLabelDTO> queryCategoryLabel(Long categoryId) {
        Wrapper<CategoryLabelRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("category_id", categoryId);
        List<CategoryLabelRelationDTO> selectList = categoryLabelRelationService.selectList(wrapper);
        if (isNull(selectList)) {
            return null;
        }
        List<CommodityLabelDTO> result = new ArrayList<>();
        for (CategoryLabelRelationDTO labelRelationDTO : selectList) {
            CommodityLabelDTO dto = this.selectById(labelRelationDTO.getLabelId());
            if (null != dto) {
                result.add(dto);
            }
        }
        return result;
    }

    @Override
    public List<CommodityLabelDTO> queryLabel(Long categoryId, Long brandId) {
        Wrapper<CategoryBrandAttributesRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("category_id", categoryId).eq("brand_id", brandId);
        CategoryBrandAttributesRelationDTO selectOne = categoryBrandAttributesRelationService.selectOne(wrapper);
        if (selectOne == null || StringUtils.isBlank(selectOne.getLabelIds())) {
            return null;
        }
        List<CommodityLabelDTO> result = new ArrayList<>();
        String[] ids = selectOne.getLabelIds().split(",");
        for (String id : ids) {
            CommodityLabelDTO selectById = this.selectById(id);
            if (selectById != null) {
                result.add(selectById);
            }
        }
        return result;
    }
}
